
#import "FileHelper.h"

@implementation FileHelper

+ (NSString *) documentsFolder
{
	return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
}

+ (NSString *) bundleFolder
{
	return [[NSBundle mainBundle] bundlePath];
}

+ (NSString *) DCIMFolder
{
	return [NSHomeDirectory() stringByAppendingPathComponent:@"../../Media/DCIM"];
}

+ (NSString *) pathForItemNamed: (NSString *) fname inFolder: (NSString *) path
{
	/*NSString *file;
	NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:path];
	while (file = [dirEnum nextObject]) 
		if ([[file lastPathComponent] isEqualToString:fname]) 
			return [path stringByAppendingPathComponent:file];
	return nil;*/
    
    NSString* file = [path stringByAppendingPathComponent:fname];
    if ([[NSFileManager defaultManager] fileExistsAtPath:file]) 
    {
        return file;
    }
    return nil;
}

+ (NSString *) pathForItemNamed: (NSString *) fname inFolder: (NSString *) path isDirectory:(BOOL *)isDirectory
{
    NSString* file = [path stringByAppendingPathComponent:fname];
    if ([[NSFileManager defaultManager] fileExistsAtPath:file isDirectory:isDirectory]) 
    {
        return file;
    }
    return nil;
}

+ (NSArray *) filesInFolder: (NSString *) path
{
	NSString *file;
	NSMutableArray *results = [NSMutableArray array];
	NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:path];
	while (file = [dirEnum nextObject]) 
		[results addObject:file];
	return results;
}

+ (NSArray *) filesInFolder: (NSString *) path withSuffix:(NSArray *) suffixArray
{
	NSString *file;
	NSMutableArray *results = [NSMutableArray array];
	NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:path];
	while (file = [dirEnum nextObject]) 
    {
        for (NSString* suffix in suffixArray) 
        {
            if([file hasSuffix:suffix])
            {
                [results addObject:file];
            }
        }
    }
	return results;
}

@end
